package com.sunspring.jbug.controller;

import com.sunspring.jbug.bean.Module;
import com.sunspring.jbug.bean.OperationLog;
import com.sunspring.jbug.bean.UserInfo;
import com.sunspring.jbug.common.LogEnum;
import com.sunspring.jbug.common.SimpleResponse;
import com.sunspring.jbug.service.ModuleService;
import com.sunspring.jbug.service.OperationLogService;
import com.sunspring.jbug.support.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wulei
 * @date 2019-03-06
 **/
@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private OperationLogService ols;

    /**
     * 新增项目模块
     * @param module Module bean
     */
    @PostMapping("/add")
    public SimpleResponse add(Module module) {
        moduleService.add(module);
        UserInfo currentUser = ContextHolder.get();
        OperationLog operationLog = OperationLog.builder()
                .type(LogEnum.PROJECT.getCode())
                .aboutId(module.getProjectId())
                .message(currentUser.getName() + "创建了模块：" + module.getModuleName())
                .build();
        ols.addLog(operationLog);
        return SimpleResponse.success();
    }

    /**
     * 查看项目模块
     * @param projectId projectId
     */
    @PostMapping("/byProject")
    public SimpleResponse byProject(Integer projectId) {
        List<Module> list = moduleService.getProjectModule(projectId);
        return SimpleResponse.success(list);
    }

    /**
     * 删除项目模块
     * @param moduleId 模块Id
     */
    @DeleteMapping("/delete")
    public SimpleResponse delete(Integer moduleId) {
        moduleService.delete(moduleId);
        return SimpleResponse.success();
    }
}
